#include <iostream>
#include "httplib.h"

void Hello(const httplib::Request &req, httplib::Response &rsp)
{
    rsp.set_content("Hello world", "text/plain");
    rsp.status = 200;
}

void Number(const httplib::Request &req, httplib::Response &rsp)
{
    auto num = req.matches[1];
    rsp.set_content(num, "text/plain");
    rsp.status = 200;

}

void Multipart(const httplib::Request &req, httplib::Response &rsp)
{
    auto ret = req.has_file("file");
    if(ret == false)
    {
        std::cout << "not file upload" << std::endl;
        rsp.status = 400; //请求格式有问题
        return ;
    }

    //获取文件字段区域数据
    const auto& file = req.get_file_value("file");
    //清空正文数据
    rsp.body.clear();
    //设置响应正文内容
    rsp.body = file.filename;
    rsp.body += "\n";
    rsp.body += file.content;
    //设置头部字段的值
    rsp.set_header("Content-Type", "text/plain");
    //响应状态码
    rsp.status = 200;
}

int main()
{
    //搭建服务器
    httplib::Server server;
    server.Get("/hi", Hello);
    server.Get(R"(/number/(\d+))", Number);
    server.Post("/multipart", Multipart);

    //启动服务器
    server.listen("0.0.0.0", 9090);
    return 0;
}